<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="Git, 博客 blog">
    <meta name="description" content="熊猫小二的博客  xmxe&#39;s blog">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- 为了引用qq空间图床文件 -->
    <meta name="referrer" content="no-referrer">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>Git | 熊猫小二</title>
    <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/blog/favicon.ico">
    <link rel="stylesheet" type="text/css" href="/blog/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/my.css">
    <link rel="stylesheet" type="text/css" href="/blog/css/loading.css">

    <script src="/blog/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 6.3.0"></head>



   
<style>
    body{
       background-image: url(/blog/medias/cover.jpg);
       background-repeat:no-repeat;
       background-size:cover;
       background-attachment:fixed;
    }
</style>



<body>
    
  <div id="loading-box">
    <div class="loading-left-bg"></div>
    <div class="loading-right-bg"></div>
    <div class="spinner-box">
      <div class="configure-border-1">
        <div class="configure-core"></div>
      </div>
      <div class="configure-border-2">
        <div class="configure-core"></div>
      </div>
      <div class="loading-word">加载中...</div>
    </div>
  </div>
  <!-- 页面加载动画 -->
  <script>
    $(document).ready(function () {
      // document.body.style.overflow = 'auto';
      document.getElementById('loading-box').classList.add("loaded")
    })
  </script>

    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/blog/" class="waves-effect waves-light">
                    
                        <img src="/blog/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">熊猫小二</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-book" style="zoom: 0.6;"></i>
      
      <span>归档</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/archives">
          
          <i class="fas fa-archive" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>归档</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/tags">
          
          <i class="fas fa-tags" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>标签</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/categories">
          
          <i class="fas fa-bookmark" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>分类</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/blog/friends" class="waves-effect waves-light">
      
      <i class="fas fa-address-book" style="zoom: 0.6;"></i>
      
      <span>友链</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="#" class="waves-effect waves-light">

      
      <i class="fas fa-user-circle" style="zoom: 0.6;"></i>
      
      <span>关于</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/blog/about">
          
          <i class="fas fa-star-of-david" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>主页</span>
        </a>
      </li>
      
      <li>
        <a href="/blog/gallery">
          
          <i class="fas fa-images" style="margin-top: -20px; zoom: 0.6;"></i>
          
          <span>相册</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
  
    <li>
      <a class="waves-effect waves-light" onclick="switchNightMode()">
        <i id="sum-moon-icon" class="fas fa-sun" style="zoom:0.65;" title="切换主题"></i>
      </a>
    </li>
  
  
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
          <img src="/blog/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">熊猫小二</div>
        <div class="logo-desc">
            
            熊猫小二的博客  xmxe&#39;s blog
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/blog/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-book"></i>
			
			归档
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/archives " style="margin-left:75px">
				  
				   <i class="fa fas fa-archive" style="position: absolute;left:50px" ></i>
			      
		          <span>归档</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/tags " style="margin-left:75px">
				  
				   <i class="fa fas fa-tags" style="position: absolute;left:50px" ></i>
			      
		          <span>标签</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/categories " style="margin-left:75px">
				  
				   <i class="fa fas fa-bookmark" style="position: absolute;left:50px" ></i>
			      
		          <span>分类</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/blog/friends" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-address-book"></i>
			
			友链
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-user-circle"></i>
			
			关于
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  >
              
                <li>

                  <a href="/blog/about " style="margin-left:75px">
				  
				   <i class="fa fas fa-star-of-david" style="position: absolute;left:50px" ></i>
			      
		          <span>主页</span>
                  </a>
                </li>
              
                <li>

                  <a href="/blog/gallery " style="margin-left:75px">
				  
				   <i class="fa fas fa-images" style="position: absolute;left:50px" ></i>
			      
		          <span>相册</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        
    </ul>
</div>


        </div>

        
    </nav>

</header>

    
<script src="/blog/libs/cryptojs/crypto-js.min.js"></script>
<script>
    (function() {
        let pwd = '';
        if (pwd && pwd.length > 0) {
            if (pwd !== CryptoJS.SHA256(prompt('请输入访问本文章的密码')).toString(CryptoJS.enc.Hex)) {
                alert('密码错误，将返回主页！');
                location.href = '/blog/';
            }
        }
    })();
</script>




<div class="bg-cover pd-header post-cover" style="background-image: url('/blog/medias/featureimages/6.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 id="post-title" class="description center-align post-title"></h1>

                    
                        <!-- <script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11"></script> -->
                        <script>
                            var typedObj = new Typed("#post-title", {
                                strings: [ 'Git' ],
                                startDelay: 300,
                                typeSpeed: 70,
                                loop: false,
                                backSpeed: 50,
                                showCursor: true
                            });
                        </script>
                    
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/blog/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
        background-color: rgb(255, 255, 255,0.7);
        border-radius: 10px;
        box-shadow: 0 10px 35px 2px rgba(0, 0, 0, .15), 0 5px 15px rgba(0, 0, 0, .07), 0 2px 5px -5px rgba(0, 0, 0, .1) !important;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
        max-height: 480px;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                          <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                          </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/blog/categories/%E6%8A%80%E6%9C%AF%E6%A0%88/" class="post-category">
                                技术栈
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                

                

                

                

                
            </div>
        </div>
        <hr class="clearfix">

        
        <!-- 是否加载使用自带的 prismjs. -->
        <link rel="stylesheet" href="/blog/libs/prism/prism.css">
        

        
        <!-- 代码块折行 -->
        <style type="text/css">
            code[class*="language-"], pre[class*="language-"] { white-space: pre-wrap !important; }
        </style>
        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <h2 id="常用命令"><a href="#常用命令" class="headerlink" title="常用命令"></a>常用命令</h2><h3 id="git-diff"><a href="#git-diff" class="headerlink" title="git diff"></a>git diff</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 可以查看当前没有add的内容修改（不在缓冲区的文件变化）</span>
<span class="token function">git</span> <span class="token function">diff</span>
<span class="token comment"># 查看已经add但没有commit的改动（在缓冲区的文件变化）</span>
<span class="token function">git</span> <span class="token function">diff</span> <span class="token parameter variable">--cached</span>
<span class="token comment"># 是上面两条命令的合并</span>
<span class="token function">git</span> <span class="token function">diff</span> HEAD
<span class="token comment"># 查看文件和另一个分支的区别</span>
<span class="token function">git</span> <span class="token function">diff</span> branch-name file-name
<span class="token comment"># 查看两次提交的区别</span>
<span class="token function">git</span> <span class="token function">diff</span> commit-id commit-id
<span class="token function">git</span> show commit-id<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-reset"><a href="#git-reset" class="headerlink" title="git reset"></a>git reset</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 将未commit的文件移出Staging区(撤销git add)</span>
<span class="token function">git</span> reset HEAD
<span class="token comment"># 撤销git add操作(已经将文件添加到暂存区)不加filename就是上一次add里面的全部撤销了</span>
<span class="token function">git</span> reset HEAD filename
<span class="token comment"># 重置代码和远程分支代码一样</span>
<span class="token function">git</span> reset <span class="token parameter variable">--hard</span> origin/master
<span class="token comment"># 回退到上个commit 删除工作空间改动代码，撤销commit，撤销git add</span>
<span class="token function">git</span> reset <span class="token parameter variable">--hard</span> HEAD^
<span class="token comment"># 回退到前3次提交之前，以此类推，回退到n次提交之前</span>
<span class="token function">git</span> reset <span class="token parameter variable">--hard</span> HEAD~3
<span class="token comment"># 回退到指定版本 --hard强制将暂存区和工作目录都同步到你指定的提交</span>
<span class="token function">git</span> reset <span class="token parameter variable">--hard</span> commitid
<span class="token comment"># 不删除工作空间改动代码，撤销commit，不撤销git add</span>
<span class="token function">git</span> reset <span class="token parameter variable">--soft</span> HEAD^
<span class="token comment"># 不删除工作空间改动代码，撤销commit，并且撤销git add操作。这个为默认参数,git reset --mixed HEAD^和git reset HEAD^效果是一样的。</span>
<span class="token function">git</span> reset <span class="token parameter variable">--mixed</span> HEAD^<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-revert"><a href="#git-revert" class="headerlink" title="git revert"></a>git revert</h3><blockquote>
<p>应用场景：有一天测试突然跟你说，你开发上线的功能有问题，需要马上撤回，否则会影响到系统使用。这时可能会想到用reset回退，可是你看了看分支上最新的提交还有其他同事的代码，用reset会把这部分代码也撤回了。由于情况紧急，又想不到好方法，还是任性的使用reset，然后再让同事把他的代码合一遍（同事听到想打人），于是你的技术形象在同事眼里一落千丈</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 回退到指定版本。与git reset --hard commitid相比区别是在我们确认了在需要回退的版本之后的提交都可以不需要的时候，我们可以直接使用git reset命令，但是当我们只是需要撤销某个版本的时候，后面的提交还需要保留，我们就可以使用git revert.revert会生成一条新的提交记录，这时会让你编辑提交信息，编辑完后:wq保存退出就好了。-n/--no edit：这个选项不会打开文本编辑器。它将直接恢复上次的提交。</span>
<span class="token function">git</span> revert <span class="token parameter variable">-n</span> commitid
<span class="token comment"># 撤销前一次commit ,此次操作之前和之后的commit都会被保留，并且会把这次撤销作为一次最新的提交</span>
<span class="token function">git</span> revert HEAD
<span class="token comment"># 撤销前前一次commit</span>
<span class="token function">git</span> revert HEAD^<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-commit"><a href="#git-commit" class="headerlink" title="git commit"></a>git commit</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># (git add -u + git commit -m ""组合)</span>
<span class="token function">git</span> commit <span class="token parameter variable">-am</span> <span class="token string">''</span>
<span class="token comment"># 修改commit信息</span>
<span class="token function">git</span> commit <span class="token parameter variable">--amend</span>
<span class="token comment"># 使用新的一次commit，来覆盖上一次commit</span>
<span class="token function">git</span> commit <span class="token parameter variable">--amend</span> <span class="token parameter variable">-m</span> <span class="token string">"message"</span>
<span class="token comment"># 提交Staging中在指定文件到本地仓库区</span>
<span class="token function">git</span> commit file1 file2 <span class="token parameter variable">-m</span> <span class="token string">"message
# 修改上次提交的用户名和邮箱
git commit --amend --author="</span>name <span class="token operator">&lt;</span>email<span class="token operator">></span>" --no-edit<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-add"><a href="#git-add" class="headerlink" title="git add"></a>git add</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> <span class="token function">add</span> <span class="token parameter variable">-A</span> <span class="token comment"># 保存所有的修改</span>
<span class="token function">git</span> <span class="token function">add</span> <span class="token builtin class-name">.</span> <span class="token comment"># 保存新的添加和修改，但是不包括删除</span>
<span class="token function">git</span> <span class="token function">add</span> <span class="token parameter variable">-u</span> <span class="token comment"># 保存修改和删除，但是不包括新建文件。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h3 id="git-branch"><a href="#git-branch" class="headerlink" title="git branch"></a>git branch</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 查看本地分支(不包括远程分支)</span>
<span class="token function">git</span> branch
<span class="token comment"># 查看远程分支</span>
<span class="token function">git</span> branch <span class="token parameter variable">-a</span>
<span class="token comment"># 列出本地所有分支 并显示最后一次提交的哈希值</span>
<span class="token function">git</span> branch <span class="token parameter variable">-v</span>
<span class="token comment"># 在-v的基础上 并且显示上游分支的名字</span>
<span class="token function">git</span> branch <span class="token parameter variable">-vv</span>
<span class="token comment"># 列出上游所有分支</span>
<span class="token function">git</span> branch <span class="token parameter variable">-r</span>
<span class="token comment"># 创建分支(未切换)</span>
<span class="token function">git</span> branch <span class="token operator">&lt;</span>name<span class="token operator">></span>
<span class="token comment"># 删除分支，不能删除当前所在分支</span>
<span class="token function">git</span> branch <span class="token parameter variable">-d</span> <span class="token operator">&lt;</span>name<span class="token operator">></span>
<span class="token comment"># 设置分支上游</span>
<span class="token function">git</span> branch --set-upstream-to origin/master
<span class="token comment"># 下载指定分支</span>
<span class="token function">git</span> clone <span class="token parameter variable">-b</span> 分支名称 git地址
<span class="token comment"># 为了节省磁盘空间，您可以使用以下命令克隆仅导致单个分支的历史记录,如果--single-branch未添加到命令中，则所有分支的历史记录将被克隆。大型存储库可能会出现此问题。</span>
<span class="token function">git</span> clone <span class="token parameter variable">-b</span> <span class="token operator">&lt;</span>branch_name<span class="token operator">></span> --single-branch <span class="token operator">&lt;</span>url<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-checkout"><a href="#git-checkout" class="headerlink" title="git checkout"></a>git checkout</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 切换分支</span>
<span class="token function">git</span> checkout <span class="token operator">&lt;</span>name<span class="token operator">></span>
<span class="token comment"># 创建+切换分支</span>
<span class="token function">git</span> checkout <span class="token parameter variable">-b</span> <span class="token operator">&lt;</span>name<span class="token operator">></span>
<span class="token comment"># 本地没有分支的情况下拉取远程分支,此命令意思为创建本地分支并关联远程分支</span>
<span class="token function">git</span> checkout <span class="token parameter variable">-b</span> local-branch origin/remote-branch
<span class="token comment"># 丢弃某个文件工作区的修改（还原修改过的文件）</span>
<span class="token function">git</span> checkout -- <span class="token function">file</span>
<span class="token comment"># 放弃本地所有修改，没有提交的可以回到未修改前版本，不包括新增删除的文件</span>
<span class="token function">git</span> checkout <span class="token builtin class-name">.</span>
<span class="token comment"># 放弃本地所有修改，包括新增删除的文件</span>
<span class="token function">git</span> checkout <span class="token builtin class-name">.</span> <span class="token operator">&amp;&amp;</span> <span class="token function">git</span> clean <span class="token parameter variable">-df</span>
<span class="token comment">#（先切换到develop分支然后把feature分支合并到develop分支）</span>
<span class="token function">git</span> checkout develop <span class="token operator">&amp;&amp;</span> <span class="token function">git</span> merge feature
<span class="token comment"># 将某个文件回滚到某个版本</span>
<span class="token function">git</span> checkout commitid <span class="token punctuation">[</span>file<span class="token punctuation">]</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-switch"><a href="#git-switch" class="headerlink" title="git switch"></a>git switch</h3><blockquote>
<p><code>git switch</code>命令是在Git 2.23.0版本中引入的，以解决<code>git checkout</code>命令职责过重的问题，并使得Git的命令更加直观和易于理解。在Git 2.23之前，<code>git checkout</code>既用于切换分支，也用于还原文件内容，很容易引起混淆。通过将<code>git checkout</code>的功能拆分，Git团队创建了两个新的、更专业的命令：<br><code>git switch</code>：专门用于在分支之间进行切换。<br><code>git restore</code>：专门用于还原文件内容。</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 切换到已存在的分支</span>
<span class="token function">git</span> switch <span class="token operator">&lt;</span>branch-name<span class="token operator">></span>
<span class="token comment"># 创建并切换到新分支</span>
<span class="token function">git</span> switch <span class="token parameter variable">-c</span> <span class="token operator">&lt;</span>new-branch-name<span class="token operator">></span>
<span class="token comment"># 从远程仓库创建并跟踪一个本地分支</span>
<span class="token function">git</span> switch <span class="token parameter variable">-c</span> <span class="token operator">&lt;</span>new-branch-name<span class="token operator">></span> <span class="token parameter variable">--track</span> <span class="token operator">&lt;</span>remote<span class="token operator">></span>/<span class="token operator">&lt;</span>branch-name<span class="token operator">></span>
<span class="token comment"># 返回到上一个分支</span>
<span class="token function">git</span> switch -<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-restore"><a href="#git-restore" class="headerlink" title="git restore"></a>git restore</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># git restore命令在工作区是不会起作用的</span>
<span class="token comment"># 取消暂存区的更改（类似于git reset HEAD &lt;file>）.此命令将暂存区中的文件恢复到最后一次提交的状态，并将其从暂存区中移除，但保留工作目录中的修改。这意味着该文件的修改将不会包含在下一次的提交</span>
<span class="token function">git</span> restore <span class="token parameter variable">--staged</span> <span class="token function">file</span>
<span class="token comment"># 此命令将工作目录中的文件恢复到最后一次提交的状态，并且不会对暂存区进行任何操作。它将丢弃工作目录中对文件的修改，恢复到最后提交的版本。</span>
<span class="token function">git</span> restore <span class="token function">file</span>
<span class="token comment"># 从指定的提交中恢复文件到工作目录</span>
<span class="token function">git</span> restore <span class="token parameter variable">--source</span><span class="token operator">=</span><span class="token operator">&lt;</span>commit<span class="token operator">></span> <span class="token operator">&lt;</span>file<span class="token operator">></span>
<span class="token comment"># 恢复所有文件到指定的提交状态</span>
<span class="token function">git</span> restore <span class="token parameter variable">--source</span><span class="token operator">=</span><span class="token operator">&lt;</span>commit<span class="token operator">></span> <span class="token builtin class-name">.</span>
<span class="token comment"># 恢复所有已删除的文件</span>
<span class="token function">git</span> restore <span class="token parameter variable">-w</span> -- *
<span class="token comment"># 丢弃暂存区和工作目录中的更改（即恢复到指定的提交状态）</span>
<span class="token function">git</span> restore <span class="token parameter variable">--staged</span> <span class="token parameter variable">--worktree</span> <span class="token operator">&lt;</span>file<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-worktree"><a href="#git-worktree" class="headerlink" title="git worktree"></a>git worktree</h3><blockquote>
<p><code>git worktree</code>命令是在Git 2.5版本中引入的，它允许在同一个仓库中创建多个工作目录（worktrees），每个工作目录可以检出不同的分支或提交。这为开发者提供了同时处理多个任务的能力，比如在不同的分支上进行开发、测试，而不需要来回切换分支</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 添加一个新的工作目录，并检出指定分支</span>
<span class="token function">git</span> worktree <span class="token function">add</span> <span class="token operator">&lt;</span>path<span class="token operator">></span> <span class="token punctuation">[</span><span class="token operator">&lt;</span>branch<span class="token operator">></span><span class="token punctuation">]</span>
<span class="token comment"># 列出所有的工作目录</span>
<span class="token function">git</span> worktree list
<span class="token comment"># 移除一个工作目录（必须先确保该目录没有未提交的更改）</span>
<span class="token function">git</span> worktree remove <span class="token operator">&lt;</span>path<span class="token operator">></span>
<span class="token comment"># 移动一个工作目录到新的位置</span>
<span class="token function">git</span> worktree move <span class="token operator">&lt;</span>current-path<span class="token operator">></span> <span class="token operator">&lt;</span>new-path<span class="token operator">></span>
<span class="token comment"># 例如，如果想要添加一个新的工作目录来检出名为feature-branch的分支，可以这样做：</span>
<span class="token function">git</span> worktree <span class="token function">add</span> <span class="token punctuation">..</span>/my-feature-worktree feature-branch
<span class="token comment"># 这将在../my-feature-worktree目录下创建一个新的工作目录，并检出feature-branch分支。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-sparse-checkout"><a href="#git-sparse-checkout" class="headerlink" title="git sparse-checkout"></a>git sparse-checkout</h3><blockquote>
<p><code>git sparse-checkout</code>是在Git 2.25.0版本中引入的，这个功能是对之前存在的稀疏检出机制的一个重大改进。通过<code>git sparse-checkout</code>，开发者可以更高效地克隆大型仓库，只检出部分文件或目录，而不是整个项目</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 要启用sparse-checkout，首先需要设置仓库以使用稀疏检出模式：</span>
<span class="token comment"># 启用sparse-checkout模式</span>
<span class="token function">git</span> sparse-checkout init
<span class="token comment"># 设置你想要包括的模式或路径</span>
<span class="token function">git</span> sparse-checkout <span class="token builtin class-name">set</span> <span class="token operator">&lt;</span>pattern<span class="token operator">></span><span class="token punctuation">..</span>.
<span class="token comment"># 例如，如果只想检出src目录及其子目录中的文件，可以这样做：</span>
<span class="token function">git</span> sparse-checkout <span class="token builtin class-name">set</span> src/
<span class="token comment"># 如果想添加多个模式或路径，可以在set命令后列出所有路径，或者分多次调用该命令。除了set命令，还可以使用add和list来管理稀疏检出模式：</span>
<span class="token comment"># 添加额外的路径到稀疏检出模式</span>
<span class="token function">git</span> sparse-checkout <span class="token function">add</span> <span class="token operator">&lt;</span>pattern<span class="token operator">></span><span class="token punctuation">..</span>.
<span class="token comment"># 列出现有的稀疏检出模式</span>
<span class="token function">git</span> sparse-checkout list
<span class="token comment"># 如果不再需要稀疏检出模式，可以通过以下命令禁用它，并恢复完整的检出状态：</span>
<span class="token comment"># 禁用sparse-checkout模式并恢复完整检出</span>
<span class="token function">git</span> sparse-checkout disable<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-range-diff"><a href="#git-range-diff" class="headerlink" title="git range-diff"></a>git range-diff</h3><blockquote>
<p><code>git range-diff</code>是在Git 2.19.0版本中引入的，用于比较两个提交范围之间的差异。它可以帮助开发者理解在一次变基（rebase）、合并（merge）或历史改写操作后，一系列提交发生了哪些变化</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 比较两个分支上的最近n个提交</span>
<span class="token function">git</span> range-diff A~n<span class="token punctuation">..</span>A B~n<span class="token punctuation">..</span>B
<span class="token comment"># 或者更常见的用法是直接指定两个范围</span>
<span class="token function">git</span> range-diff A<span class="token punctuation">..</span>B C<span class="token punctuation">..</span>D
<span class="token comment"># 假设origin/feature是变基之前的远程分支状态,而feature是变基之后的本地分支状态</span>
<span class="token function">git</span> range-diff origin/feature<span class="token punctuation">..</span>feature~n feature~n<span class="token punctuation">..</span>feature<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-maintenance"><a href="#git-maintenance" class="headerlink" title="git maintenance"></a>git maintenance</h3><blockquote>
<p><code>git maintenance</code>是在Git 2.30.0版本中引入的，用于管理和自动化各种维护任务的命令。这个命令旨在简化和优化仓库的维护工作，通过提供一组预定义的任务来帮助保持仓库的健康状态和高效性能。常见的维护任务包括：<br>gc：运行完整的垃圾收集，包括压缩对象数据库。<br>commit-graph：构建或更新提交图文件以加速提交历史查询。<br>loose-objects：清理松散对象并将其打包。<br>incremental-repack：逐步重新打包对象以优化存储。<br>prefetch：预先获取远程分支的新数据，以加速未来的克隆和拉取操作。</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 启用自动维护</span>
<span class="token function">git</span> maintenance start
<span class="token comment"># 禁用自动维护</span>
<span class="token function">git</span> maintenance stop
<span class="token comment"># 执行所有配置的维护任务</span>
<span class="token function">git</span> maintenance run
<span class="token comment"># 执行特定类型的维护任务</span>
<span class="token function">git</span> maintenance run <span class="token parameter variable">--task</span><span class="token operator">=</span><span class="token operator">&lt;</span>task<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>配置自动维护计划：可以通过配置文件设置哪些任务应该被定期执行以及它们的执行频率。例如，在<code>.git/config</code>文件中添加如下内容：</p>
<pre class="line-numbers language-config" data-language="config"><code class="language-config">[maintenance &quot;daily&quot;]
    task &#x3D; prefetch
    task &#x3D; loose-objects
[maintenance &quot;hourly&quot;]
    task &#x3D; commit-graph
[maintenance &quot;weekly&quot;]
    task &#x3D; incremental-repack
[maintenance &quot;monthly&quot;]
    task &#x3D; gc<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>
<p>然后启用这些计划：</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> maintenance start <span class="token parameter variable">--schedule</span><span class="token operator">=</span>daily
<span class="token function">git</span> maintenance start <span class="token parameter variable">--schedule</span><span class="token operator">=</span>hourly
<span class="token function">git</span> maintenance start <span class="token parameter variable">--schedule</span><span class="token operator">=</span>weekly
<span class="token function">git</span> maintenance start <span class="token parameter variable">--schedule</span><span class="token operator">=</span>monthly<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-merge-amp-git-rebase"><a href="#git-merge-amp-git-rebase" class="headerlink" title="git merge &amp; git rebase"></a>git merge &amp; git rebase</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 合并指定分支到当前分支</span>
<span class="token function">git</span> merge <span class="token operator">&lt;</span>name<span class="token operator">></span>
<span class="token comment"># 将指定分支合并到当前分支</span>
<span class="token function">git</span> rebase branch-name
<span class="token comment"># 执行commit id将rebase停留在指定commit处</span>
<span class="token function">git</span> rebase <span class="token parameter variable">-i</span> commit-id
<span class="token comment"># 执行commit id将rebase停留在 项目首次commit处</span>
<span class="token function">git</span> rebase <span class="token parameter variable">-i</span> <span class="token parameter variable">--root</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<blockquote>
<p><strong>git rebase和git merge区别</strong><br>采用merge和rebase后，git log的区别，merge命令不会保留merge的分支的commit，rebase会保留所有的commit：rebase会把你当前分支的commit放到公共分支的最后面,所以叫变基。就好像你从公共分支又重新拉出来这个分支一样。举例:如果你从master拉了个feature分支出来,然后你提交了几个commit,这个时候刚好有人把他开发的东西合并到master了,这个时候master就比你拉分支的时候多了几个commit,如果这个时候你rebase master的话，就会把你当前的几个commit，放到那个人commit的后面。merge会把公共分支和你当前的commit合并在一起，形成一个新的commit提交<br>处理冲突的方式：（一股脑）使用merge命令合并分支，解决完冲突，执行git add .和git commit -m ‘fix conflict’。这个时候会产生一个commit。（交互式）使用rebase命令合并分支，解决完冲突，执行git add .和git rebase –continue，不会产生额外的commit。这样的好处是干净，分支上不会有无意义的解决分支的commit；坏处，如果合并的分支中存在多个commit，需要重复处理多次冲突。</p>
</blockquote>
<blockquote>
<p><strong>git pull和git pull –rebase区别</strong><br>git pull做了两个操作分别是‘获取’和合并。所以加了rebase就是以rebase的方式进行合并分支得到一条干净的分支流。</p>
</blockquote>
<blockquote>
<p><strong>git merge和git merge –no-ff的区别</strong><br>自己尝试merge命令后，发现merge时并没有产生一个commit。不是说merge时会产生一个merge commit吗？注意：只有在冲突的时候，解决完冲突才会自动产生一个commit。如果想在没有冲突的情况下也自动生成一个commit，记录此次合并就可以用：git merge –no-ff命令,如果不加–no-ff则被合并的分支之前的commit都会被抹去，只会保留一个解决冲突后的merge commit。</p>
</blockquote>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/T_8bkWI-JSP5ixdVIvVAGQ">合并代码还在用git merge？我们都用git rebase！</a><br><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/CBz0ea6m623GtuTX5UkeQQ">新来个技术总监，禁止我们用Git的rebase</a></p>
</blockquote>
<h3 id="git-tag"><a href="#git-tag" class="headerlink" title="git tag"></a>git tag</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 查看所有标签，可以知道历史版本的tag</span>
<span class="token function">git</span> tag
<span class="token comment"># 打标签，默认为HEAD。比如git tag v1.0</span>
<span class="token function">git</span> tag <span class="token operator">&lt;</span>tagName<span class="token operator">></span>
<span class="token comment"># 根据版本号打上标签</span>
<span class="token function">git</span> tag <span class="token operator">&lt;</span>tagName<span class="token operator">></span> commit_id
<span class="token comment"># 创建带说明的标签。-a指定标签名，-m指定说明文字</span>
<span class="token function">git</span> tag <span class="token parameter variable">-a</span> <span class="token operator">&lt;</span>tagName<span class="token operator">></span> <span class="token parameter variable">-m</span> <span class="token string">"&lt;说明>"</span>
<span class="token comment"># 查看标签信息</span>
<span class="token function">git</span> show <span class="token operator">&lt;</span>tagName<span class="token operator">></span>
<span class="token comment"># 删除标签</span>
<span class="token function">git</span> tag <span class="token parameter variable">-d</span> <span class="token operator">&lt;</span>tagName<span class="token operator">></span>
<span class="token comment"># 推送某个标签到远程</span>
<span class="token function">git</span> push origin <span class="token operator">&lt;</span>tagname<span class="token operator">></span>
<span class="token comment"># 一次性推送全部尚未推送到远程的本地标签</span>
<span class="token function">git</span> push origin <span class="token parameter variable">--tags</span>
<span class="token comment"># 这会将空引用推送到远程仓库，从而删除名为&lt;tag-name>的远程标签。</span>
<span class="token function">git</span> push origin :refs/tags/<span class="token operator">&lt;</span>tag-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-stash"><a href="#git-stash" class="headerlink" title="git stash"></a>git stash</h3><blockquote>
<p>应用场景：某一天你正在feature分支开发新需求，突然产品经理跑过来说线上有bug，必须马上修复。而此时你的功能开发到一半，于是你急忙想切到master分支，然后你就会看到以下报错：Your local changes to the following…因为当前有文件更改了，需要提交commit保持工作区干净才能切分支。由于情况紧急，你只有急忙commit上去，commit信息也随便写了个“暂存代码”，于是该分支提交记录就留了一条黑历史，如果你学会stash，就不用那么狼狈了。你只需要：git stash就这么简单，代码就被存起来了。当你修复完线上问题，切回feature分支，想恢复代码也只需要：git stash apply,但是恢复后，stash内容并不删除，需要用git stash drop来删除</p>
</blockquote>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 保存当前未commit的代码</span>
<span class="token function">git</span> stash
<span class="token comment"># 保存当前未commit的代码并添加备注</span>
<span class="token function">git</span> stash save <span class="token string">"备注的内容"</span>
<span class="token comment"># 列出stash的所有记录</span>
<span class="token function">git</span> stash list
<span class="token comment"># 删除stash的所有记录</span>
<span class="token function">git</span> stash <span class="token function">clear</span>
<span class="token comment"># 应用最近一次的stash</span>
<span class="token function">git</span> stash apply
<span class="token comment"># 应用最近一次的stash，随后删除该记录</span>
<span class="token function">git</span> stash pop
<span class="token comment"># 删除最近的一次stash</span>
<span class="token function">git</span> stash drop

<span class="token comment"># 当有多条stash，可以指定操作stash，首先使用stash list列出所有记录：</span>
<span class="token function">git</span> stash list 
<span class="token comment"># stash@&#123;0&#125;: WIP on ...</span>
<span class="token comment"># stash@&#123;1&#125;: WIP on ...</span>
<span class="token comment"># stash@&#123;2&#125;: On ...</span>

<span class="token comment"># 应用第二条记录：</span>
<span class="token function">git</span> stash apply stash@<span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">&#125;</span>
<span class="token comment"># 删除stash@&#123;1&#125;存储的内容</span>
<span class="token function">git</span> stash drop stash@<span class="token punctuation">&#123;</span><span class="token number">1</span><span class="token punctuation">&#125;</span>
<span class="token comment"># pop，drop同理。</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-config"><a href="#git-config" class="headerlink" title="git config"></a>git config</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> config <span class="token parameter variable">--gloabl</span> http.postBuffer <span class="token number">524288000</span>
<span class="token function">git</span> config <span class="token parameter variable">--gloabl</span> http.sslVerify <span class="token string">"false"</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> user.name <span class="token string">"your name"</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> user.email <span class="token string">"your email"</span>
<span class="token comment"># 保存密码</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> credential.helper store
<span class="token comment"># 配置git图形界面编码为utf-8</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> <span class="token assign-left variable">gui.encoding</span><span class="token operator">=</span>utf-8
<span class="token comment"># 设置当前项目提交代码的用户名 </span>
<span class="token function">git</span> config user.name name<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-cherry-pick"><a href="#git-cherry-pick" class="headerlink" title="git cherry-pick"></a>git cherry-pick</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 把a分支的一个提交复制到b分支</span>
<span class="token comment"># 首先复制a分支的commitid然后切换到b分支执行</span>
<span class="token function">git</span> cherry-pick commitid
<span class="token comment"># 一次转移多个提交：将commit1和commit2两个提交应用到当前分支。</span>
<span class="token function">git</span> cherry-pick commit1 commit2
<span class="token comment"># 多个连续的commit，也可区间复制：将commit1到commit2这个区间的commit都应用到当前分支（包含commit1、commit2），commit1是最早的提交。</span>
<span class="token function">git</span> cherry-pick commit1^<span class="token punctuation">..</span>commit2
<span class="token comment"># 在cherry-pick多个commit时，可能会遇到代码冲突，这时cherry-pick会停下来，让用户决定如何继续操作,这时需要解决代码冲突，重新提交到暂存区,然后使用cherry-pick --continue让cherry-pick继续进行下去。</span>
<span class="token function">git</span> cherry-pick <span class="token parameter variable">--continue</span>
<span class="token comment"># 但有时候可能需要在代码冲突后，放弃或者退出流程：放弃cherry-pick：</span>
<span class="token function">git</span> cherry-pick <span class="token parameter variable">--abort</span>
<span class="token comment"># 回到操作前的样子，就像什么都没发生过。</span>
<span class="token function">git</span> cherry-pick <span class="token parameter variable">--quit</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-push"><a href="#git-push" class="headerlink" title="git push"></a>git push</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 删除远程分支</span>
<span class="token function">git</span> push origin :beanch-name
<span class="token comment"># 使用git push命令并加上--delete 选项来删除指定的远程分支</span>
<span class="token function">git</span> push origin <span class="token parameter variable">--delete</span> branch-name
<span class="token comment">#  删除远程标签</span>
<span class="token function">git</span> push origin <span class="token parameter variable">--delete</span> tag-name
<span class="token comment"># 删除远程标签（需要先删除本地标签）</span>
<span class="token function">git</span> push origin :refs/tags/tag-name
<span class="token comment"># 上传本地仓库到远程分支</span>
<span class="token function">git</span> push remote branch-name
<span class="token comment"># 强行推送当前分支到远程分支</span>
<span class="token function">git</span> push remote branch-name <span class="token parameter variable">--force</span>
<span class="token comment"># 推送所有分支到远程仓库</span>
<span class="token function">git</span> push remote <span class="token parameter variable">--all</span>
<span class="token comment"># 推送所有标签</span>
<span class="token function">git</span> push <span class="token parameter variable">--tags</span>
<span class="token comment"># 推送指定标签</span>
<span class="token function">git</span> push origin tag-name
<span class="token comment"># 将本地dev分支push到远程master分支</span>
<span class="token function">git</span> push origin dev:master<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git-fetch"><a href="#git-fetch" class="headerlink" title="git fetch"></a>git fetch</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 更新所有跟踪的远程分支,这将会从默认的远程仓库(通常是origin)拉取所有新的数据</span>
<span class="token function">git</span> fetch
<span class="token comment"># 指定一个不同的远程仓库</span>
<span class="token function">git</span> fetch <span class="token operator">&lt;</span>远程仓库名<span class="token operator">></span>
<span class="token comment"># 只获取某个特定的分支而不是所有分支的更新</span>
<span class="token function">git</span> fetch <span class="token operator">&lt;</span>远程仓库名<span class="token operator">></span> <span class="token operator">&lt;</span>分支名<span class="token operator">></span>
<span class="token comment"># 当你在远程删除了某些分支后，本地仍然可能保留着对这些已删除分支的引用。使用`--prune` 选项可以在获取最新数据的同时移除那些不再存在于远程仓库中的本地远程跟踪分支</span>
<span class="token function">git</span> fetch <span class="token parameter variable">--prune</span>
<span class="token comment"># 或者针对特定远程仓库</span>
<span class="token function">git</span> fetch <span class="token operator">&lt;</span>远程仓库名<span class="token operator">></span> <span class="token parameter variable">--prune</span>
<span class="token comment"># 获取更新后，你可以选择将这些更改合并到你的当前分支中，使用`git merge`或者`git rebase`。例如，假设你已经从origin拉取了更新，现在想要将这些更新合并到当前分支</span>
<span class="token function">git</span> merge origin/目标分支 <span class="token operator">||</span> <span class="token function">git</span> rebase origin/目标分支<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="常用操作"><a href="#常用操作" class="headerlink" title="常用操作"></a>常用操作</h2><h3 id="删除全部历史提交记录"><a href="#删除全部历史提交记录" class="headerlink" title="删除全部历史提交记录"></a>删除全部历史提交记录</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 创建孤立分支,没有以前的提交记录</span>
<span class="token function">git</span> checkout <span class="token parameter variable">--orphan</span> <span class="token operator">&lt;</span>name<span class="token operator">></span>
<span class="token comment"># 切换到一个脱离主分支的另外一条全新主分支，不用太在意叫什么，因为后面还会修改分支名称</span>
<span class="token function">git</span> checkout <span class="token parameter variable">--orphan</span> latest_branch
<span class="token comment"># 暂存所有改动过的文件，内容为当前旧分支的最新版本所有文件</span>
<span class="token function">git</span> <span class="token function">add</span> <span class="token parameter variable">-A</span>
<span class="token comment">#提交更改</span>
<span class="token function">git</span> commit <span class="token parameter variable">-am</span> <span class="token string">"commit message"</span>
<span class="token comment">#删除原始主分支</span>
<span class="token function">git</span> branch <span class="token parameter variable">-D</span> main
<span class="token comment">#将当前分支重命名为 main</span>
<span class="token function">git</span> branch <span class="token parameter variable">-m</span> main
<span class="token comment">#最后，强制更新您的存储库</span>
<span class="token function">git</span> push <span class="token parameter variable">-f</span> origin main<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="删除部分历史提交记录"><a href="#删除部分历史提交记录" class="headerlink" title="删除部分历史提交记录"></a>删除部分历史提交记录</h3><ol>
<li>运行<code>git log</code>来查找你要删除的提交记录的hash值(commit id)，记录下来。</li>
<li>执行交互式rebase命令：<code>git rebase -i commit_id</code><blockquote>
<p>如果想要删除最近3次历史提交: <code>git rebase -i HEAD~3</code></p>
</blockquote>
</li>
<li>进入编辑模式，将要删除的commit_id前的pick修改为drop。保存并退出编辑模式。</li>
<li>推送更新到远程仓库：<code>git push --force(或git p)</code></li>
</ol>
<h3 id="远程分支重命名"><a href="#远程分支重命名" class="headerlink" title="远程分支重命名"></a>远程分支重命名</h3><p>假设你当前已经将该分支推送到远程了，这种情况修改起来要稍微多几步</p>
<pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 方案1:先重命名本地分支，然后推送到远程分支</span>
<span class="token comment"># 1.先重命名本地分支</span>
<span class="token function">git</span> branch <span class="token parameter variable">-m</span> 旧分支名称 新分支名称
<span class="token comment"># 2.删除远程分支(如果删除的是默认分支的话会失败，需要先更改默认分支)</span>
<span class="token function">git</span> push <span class="token parameter variable">--delete</span> origin 旧分支名称
<span class="token comment"># 3.上传新修改名称的本地分支</span>
<span class="token function">git</span> push origin 新分支名称
<span class="token comment"># 4.修改后的本地分支关联远程分支</span>
<span class="token function">git</span> branch --set-upstream-to origin/新分支名称

<span class="token comment"># 方案2(未测试):先修改远程仓库分支，然后与本地仓库同步</span>
<span class="token function">git</span> branch <span class="token parameter variable">-m</span> master <span class="token number">2021</span>.x
<span class="token comment"># 获取源分支</span>
<span class="token function">git</span> fetch origin
<span class="token comment"># 切换源分支为远程分支</span>
<span class="token function">git</span> branch <span class="token parameter variable">-u</span> origin/2021.x <span class="token number">2021</span>.x
<span class="token comment"># 设置远程分支</span>
<span class="token function">git</span> remote set-head origin <span class="token parameter variable">-a</span>
<span class="token comment"># 方案2另一种实现(未测试):先修改远程仓库分支，然后与本地仓库同步</span>
<span class="token comment"># 首先，在本地仓库中切换到需要同步的分支上：这里&lt;branch-name>是需要同步的分支名称</span>
<span class="token function">git</span> checkout <span class="token operator">&lt;</span>branch-name<span class="token operator">></span>
<span class="token comment"># 接下来，从远程仓库中获取最新的分支列表和分支状态信息：这会更新本地仓库中的远程分支信息。</span>
<span class="token function">git</span> fetch
<span class="token comment"># 然后，使用以下命令来重置本地分支到远程分支的最新状态：这将强制将本地分支指向远程分支的最新状态。</span>
<span class="token function">git</span> reset <span class="token parameter variable">--hard</span> origin/<span class="token operator">&lt;</span>branch-name<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="下载所有分支"><a href="#下载所有分支" class="headerlink" title="下载所有分支"></a>下载所有分支</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># git clone下载的是默认分支,分支较少的话可以使用git branch -a查看所有远程分支然后使用'git checkout 分支名'来下载其他分支。如果分支较多的话使用--bare,裸仓库(bare repository)指的是除了git仓库不包含其他工作文件的仓库，可以通过git clone --bare来生成。</span>
<span class="token function">git</span> clone <span class="token parameter variable">--bare</span> https://github.com/xx/project.git .git
<span class="token comment"># 或者git config --unset core.bare</span>
<span class="token function">git</span> config <span class="token parameter variable">--bool</span> core.bare <span class="token boolean">false</span>
<span class="token comment"># 上面的命令执行完,再执行该命令,就可以看到仓库里面的内容了</span>
<span class="token function">git</span> reset <span class="token parameter variable">--hard</span>
<span class="token comment"># 或者切换到指定分支</span>
<span class="token function">git</span> checkout 指定分支名<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="上传git仓库"><a href="#上传git仓库" class="headerlink" title="上传git仓库"></a>上传git仓库</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 创建.git目录</span>
<span class="token function">git</span> init
<span class="token function">git</span> <span class="token function">add</span> <span class="token parameter variable">-A</span>
<span class="token function">git</span> commit <span class="token parameter variable">-m</span> <span class="token string">"init"</span>
<span class="token function">git</span> remote <span class="token function">add</span> origin https://github.com/xmxe/project.git
<span class="token comment"># 断开连接</span>
<span class="token function">git</span> remote remove origin
<span class="token comment"># 增加一个新的远程仓库</span>
<span class="token function">git</span> remote <span class="token function">add</span> name url
<span class="token comment"># 获取指定远程仓库的详细信息</span>
<span class="token function">git</span> remote show origin

<span class="token function">git</span> pull <span class="token parameter variable">--rebase</span> origin master
<span class="token comment"># git push --set-upstream origin master可以简写成git push -u</span>
<span class="token function">git</span> push <span class="token parameter variable">-u</span> origin master

<span class="token comment"># 本地生成ssh密钥</span>
ssh-keygen <span class="token parameter variable">-t</span> rsa <span class="token parameter variable">-C</span> <span class="token string">"你的邮箱"</span>生成sshkey
<span class="token function">git</span> remote set-url origin git@github.com:xmxe/springcloud.git<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="git删除github文件夹但不删除本地的-以-idea为例"><a href="#git删除github文件夹但不删除本地的-以-idea为例" class="headerlink" title="git删除github文件夹但不删除本地的 以.idea为例"></a>git删除github文件夹但不删除本地的 以.idea为例</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token function">git</span> <span class="token function">rm</span> <span class="token parameter variable">-r</span> <span class="token parameter variable">--cached</span> .idea <span class="token comment"># --cached不会把本地的.idea删除</span>
<span class="token function">git</span> commit <span class="token parameter variable">-m</span> <span class="token string">'delete .idea dir'</span>
<span class="token function">git</span> push <span class="token parameter variable">-u</span> origin master<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span></span></code></pre>

<h3 id="git删除大文件"><a href="#git删除大文件" class="headerlink" title="git删除大文件"></a>git删除大文件</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 显示10个最大的文件id列表</span>
<span class="token function">git</span> verify-pack <span class="token parameter variable">-v</span> .git/objects/pack/pack-*.idx <span class="token operator">|</span> <span class="token function">sort</span> <span class="token parameter variable">-k</span> <span class="token number">3</span> <span class="token parameter variable">-n</span> <span class="token operator">|</span> <span class="token function">tail</span> <span class="token parameter variable">-10</span>
<span class="token comment"># 根据文件id查询文件路径</span>
<span class="token function">git</span> rev-list <span class="token parameter variable">--objects</span> <span class="token parameter variable">--all</span> <span class="token operator">|</span> <span class="token function">grep</span> 08a7475
<span class="token comment"># 删除文件的历史记录</span>
<span class="token function">git</span> filter-branch <span class="token parameter variable">--force</span> --index-filter <span class="token string">'git rm --cached --ignore-unmatch 文件名'</span> --prune-empty --tag-name-filter <span class="token function">cat</span> -- <span class="token parameter variable">--all</span>

<span class="token function">git</span> filter-branch --index-filter <span class="token comment"># 让每个提交的文件都复制到索引(.git/index)中 然后运行过滤器命令：git rm --cached --ignore-unmatch文件名，让每个提交都删除掉“文件名”文件,然后--prune-empty 把空的提交“修剪”掉,然后--tag-name-filter cat把每个tag保持原名字，指向修改后的对应提交,最后-- --all将所有ref（包括branch、tag）都执行上面的重写</span>

<span class="token comment"># 删除缓存下来的ref和git操作记录</span>
<span class="token function">git</span> for-each-ref <span class="token parameter variable">--format</span><span class="token operator">=</span><span class="token string">'delete %(refname)'</span> refs/original <span class="token operator">|</span> <span class="token function">git</span> update-ref <span class="token parameter variable">--stdin</span>
<span class="token function">git</span> reflog expire <span class="token parameter variable">--expire</span><span class="token operator">=</span>now <span class="token parameter variable">--all</span>

<span class="token comment"># 垃圾回收</span>
<span class="token comment"># 上面2步把大文件的索引都切断了，这个时候进行垃圾回收，就可以很明显看到效果了</span>
<span class="token function">git</span> gc <span class="token parameter variable">--prune</span><span class="token operator">=</span>now
<span class="token comment"># 把.git里面的修改推上去,这个时候普通的push是不行的，需要强推</span>
<span class="token function">git</span> push <span class="token parameter variable">--force</span>
<span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="忽略已经纳入git版本控制的文件更改"><a href="#忽略已经纳入git版本控制的文件更改" class="headerlink" title="忽略已经纳入git版本控制的文件更改"></a>忽略已经纳入git版本控制的文件更改</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 忽略单个文件的更改</span>
<span class="token function">git</span> update-index --assume-unchanged <span class="token operator">&lt;</span>file<span class="token operator">></span>
<span class="token comment"># 恢复跟踪更改</span>
<span class="token function">git</span> update-index --no-assume-unchanged <span class="token operator">&lt;</span>file<span class="token operator">></span>
<span class="token comment"># 更彻底的忽略</span>
<span class="token function">git</span> update-index --skip-worktree <span class="token operator">&lt;</span>file<span class="token operator">></span>
<span class="token function">git</span> update-index --no-skip-worktree <span class="token operator">&lt;</span>file<span class="token operator">></span>
<span class="token comment"># Linux查看被忽略更改的文件</span>
<span class="token function">git</span> ls-files <span class="token parameter variable">-v</span> <span class="token operator">|</span> <span class="token function">grep</span> <span class="token string">'^[a-z]'</span>
<span class="token comment"># 小写字母表示被--assume-unchanged忽略的文件，S表示被--skip-worktree忽略的文件。选择哪种方法取决于你的具体需求，--skip-worktree通常比--assume-unchanged更可靠。</span>
<span class="token comment"># PowerShell版</span>
<span class="token comment"># 查看所有被assume-unchanged的文件（小写标记）</span>
<span class="token function">git</span> ls-files <span class="token parameter variable">-v</span> <span class="token operator">|</span> Where-Object <span class="token punctuation">&#123;</span> <span class="token variable">$_</span> <span class="token parameter variable">-match</span> <span class="token string">'^[a-z]'</span> <span class="token punctuation">&#125;</span>
<span class="token comment"># 查看所有被skip-worktree的文件（S标记）</span>
<span class="token function">git</span> ls-files <span class="token parameter variable">-v</span> <span class="token operator">|</span> Where-Object <span class="token punctuation">&#123;</span> <span class="token variable">$_</span> <span class="token parameter variable">-match</span> <span class="token string">'^S'</span> <span class="token punctuation">&#125;</span>

<span class="token comment"># --assume-unchanged：告诉Git假设文件未更改，性能优化用</span>
<span class="token comment"># --skip-worktree：告诉Git完全忽略文件的更改，即使文件确实有变化</span>
<span class="token comment"># .gitignore:只对未跟踪文件有效，对已跟踪文件需要先git rm --cached.或对于目录：git rm --cached -r &lt;directory></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h3 id="设置Git短命令"><a href="#设置Git短命令" class="headerlink" title="设置Git短命令"></a>设置Git短命令</h3><pre class="line-numbers language-bash" data-language="bash"><code class="language-bash"><span class="token comment"># 方式一</span>
<span class="token function">git</span> config <span class="token parameter variable">--global</span> alias.ps push
<span class="token comment"># 方式二 打开全局配置文件</span>
<span class="token function">vim</span> ~/.gitconfig
<span class="token comment"># 写入内容</span>
<span class="token punctuation">[</span>alias<span class="token punctuation">]</span> 
        co <span class="token operator">=</span> checkout
        <span class="token function">ps</span> <span class="token operator">=</span> push
        pl <span class="token operator">=</span> pull
        mer <span class="token operator">=</span> merge --no-ff
        <span class="token function">cp</span> <span class="token operator">=</span> cherry-pick
<span class="token comment"># 使用</span>
<span class="token comment"># 等同于 git cherry-pick &lt;commitHash></span>
<span class="token function">git</span> <span class="token function">cp</span> <span class="token operator">&lt;</span>commitHash<span class="token operator">></span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>

<h2 id="相关文章"><a href="#相关文章" class="headerlink" title="相关文章"></a>相关文章</h2><table>
<thead>
<tr>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ct6GWiE_hzoXUNeriLAnng">Git不要只会pull和push，试试这5条提高效率的命令</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/MM7sQiFPh2vIuGvg1-813Q">Git各指令的本质，真是通俗易懂啊！</a></th>
<th align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/csEgAjJwH75_IvAnFBIuvw">Git科普文，Git基本原理&amp;各种骚操作</a></th>
</tr>
</thead>
<tbody><tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/xoyQ4TzVKLQb2VjZJLUqFQ">一文快速掌握Git用法</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/wtPizZ3RlwY3Ex1Lc3Pf4A">精心整理：Git从入门到精通、包教包会、收藏一下、随时学习</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/OchvVMGoBzSFWhou4WhrWw">大牛总结的Git使用技巧，写得太好了！</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/q6tI0qctvciJhNz_5KLx-w">通过.git目录深入理解Git！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/KwTsWyFh07iYdfINqo9UTg">Git命令全方位学习</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/TW_qUWRVEnberle5q0ws9Q">图解Git，一目了然！</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/dYiWQQ5PsSS7F7z7cDLEuw">Git代码防丢指南，再也不怕丢失代码了！</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/XB_G7TZqBX8r3CJlvqA0Cg">20个最常用的Git命令，你都会用吗？</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/Fa8mmQpNZ1S80Kg9Oyocbw">45个GIT经典操作场景，专治不会合代码</a></td>
</tr>
<tr>
<td align="center"><a target="_blank" rel="noopener" href="https://github.com/521xueweihan/git-tips">Git的奇技淫巧</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/ADzad6e6uTF0vpZ903SJ-A">如何配置SSH管理多个Git仓库和以及多个Github账号</a></td>
<td align="center"><a target="_blank" rel="noopener" href="https://mp.weixin.qq.com/s/L5NEAOgoee8TQ0bOpp6PgQ">那些年我们错过的Git指南</a></td>
</tr>
</tbody></table>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/blog/about" rel="external nofollow noreferrer">xmxe</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="https://xmxe.github.io/blog/posts/30bd6b4cbf9a/">https://xmxe.github.io/blog/posts/30bd6b4cbf9a/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/blog/about" target="_blank">xmxe</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            <span class="chip bg-color">无标签</span>
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/blog/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="qq,qzone,wechat,weibo,douban" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/blog/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        
                            <img src="/blog/medias/reward/alipay.jpg" class="reward-img" alt="支付宝打赏二维码">
                        
                    </div>
                    <div id="wechat">
                        
                            <img src="/blog/medias/reward/wechat.jpg" class="reward-img" alt="微信打赏二维码">
                        
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/blog/posts/be5a099f7894/">
                    <div class="card-image">
                        
                        <img src="https://pic1.zhimg.com/v2-716588f96320420369eb5a5734852aa5_1440w.jpg" class="responsive-img" alt="JVM知识点">
                        
                        <span class="card-title">JVM知识点</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            JVM内存区域、垃圾回收、调优、JVM工具、参数等相关命令
                        

                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-27
                            -->

                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                <i class="fas fa-bookmark fa-fw icon-category"></i>
                                
                                    <a href="/blog/categories/Java/" class="post-category">
                                        Java
                                    </a>
                                
                            

                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/blog/posts/2ab37fc4e673/">
                    <div class="card-image">
                        
                        <img src="https://picx1.zhimg.com/v2-e68d524210343613129267bd2cb75a0d_1440w.jpg" class="responsive-img" alt="Nginx">
                        
                        <span class="card-title">Nginx</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            Nginx安装离线安装下载
# 下载nginx:
wget http://nginx.org/download/nginx-1.8.1.tar.gz
# 下载openssl:
wget https://www.openssl.org/sou
                        
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <!--
                            <i class="far fa-clock fa-fw icon-date"></i>2022-09-27
                            -->
                            
                                <i class="fas fa-user fa-fw"></i>
                                <a href="/blog/about" >
                                    xmxe
                                </a>
                            
                        </span>
                        <span class="publish-author">
                            
                                
                                <a href="/blog/tags/%E5%AE%89%E8%A3%85/">
                                    <span class="chip bg-color">安装</span>
                                </a>
                                
                            
                            
                        </span>
                    </div>
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/blog/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/blog/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/blog/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
        <link rel="stylesheet" href="/blog/libs/aplayer/APlayer.min.css">
<style>
    .aplayer .aplayer-lrc p {
        
        display: none;
        
        font-size: 12px;
        font-weight: 700;
        line-height: 16px !important;
    }

    .aplayer .aplayer-lrc p.aplayer-lrc-current {
        
        display: none;
        
        font-size: 15px;
        color: #42b983;
    }

    
    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
        left: -66px !important;
    }

    .aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
        left: 0px !important;
    }

    
</style>
<div class="">
    
    <div class="row">
        <meting-js class="col l8 offset-l2 m10 offset-m1 s12"
                   server="netease"
                   type="song"
                   id="569200213"
                   fixed='true'
                   autoplay='false'
                   theme='#42b983'
                   loop='all'
                   order='random'
                   preload='auto'
                   volume='0.7'
                   list-folded='true'
        >
        </meting-js>
    </div>
</div>

<script src="/blog/libs/aplayer/APlayer.min.js"></script>
<!-- <script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script> -->

    
    <div class="container row center-align" style="margin-bottom: 0px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
            
                <span id="year">2022-2025
                </span>
            
            

            <a href="/blog/about" target="_blank">
                xmxe
            </a>
            |&nbsp;Powered by&nbsp;
            <a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;
            <a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>

            
            
            
            
            
            

            
            <br>

            
            <br>

            
        </div>
        <div class="col s12 m4 l4 social-link2 ">
    <a href="https://github.com/xmxe" class="tooltipped" target="_blank" data-tooltip="GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="https://gitee.com/xmxe" class="tooltipped" target="_blank" data-tooltip="码云" data-position="top" data-delay="50">
        <svg width="19" height="19" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" style="position: relative; top: 2px; left: 0.5px;">
            <path d="M512 1024C230.4 1024 0 793.6 0 512S230.4 0 512 0s512 230.4 512 512-230.4 512-512 512z m259.2-569.6H480c-12.8 0-25.6 12.8-25.6 25.6v64c0 12.8 12.8 25.6 25.6 25.6h176c12.8 0 25.6 12.8 25.6 25.6v12.8c0 41.6-35.2 76.8-76.8 76.8h-240c-12.8 0-25.6-12.8-25.6-25.6V416c0-41.6 35.2-76.8 76.8-76.8h355.2c12.8 0 25.6-12.8 25.6-25.6v-64c0-12.8-12.8-25.6-25.6-25.6H416c-105.6 0-188.8 86.4-188.8 188.8V768c0 12.8 12.8 25.6 25.6 25.6h374.4c92.8 0 169.6-76.8 169.6-169.6v-144c0-12.8-12.8-25.6-25.6-25.6z" fill="#fff">
            </path>
        </svg>
    </a>

















    
        
          <a href="/blog/download" class="tooltipped" target="_blank" data-tooltip="下载" data-position="top" data-delay="50">
            <i class="fas fa-download"></i>
          </a>
        
    



    <style>
  .mobiledevice {
    display: none !important;
  }

  footer .wechat_qrcode {
    position: fixed;
  }

  /*微信二维码*/
  .wechat_qrcode {
    position: absolute;
    margin-left: 10px;
    bottom: 10px;
    background: url("/blog/medias/xcx.png");
    zoom:40%;
  }

  .wechat:hover .wechat_qrcode {
    width: 430px;
    height: 430px;
    animation: move 0.4s linear 1 normal;
  }

  @keyframes move {
    0% {
      transform: translate(100px, 0);
      opacity: 0;
    }
    50% {
      transform: translate(50px, 0);
      opacity: 0.5;
    }
    100% {
      transform: translate(0, 0);
      opacity: 1;
    }
  }

  @media only screen and (max-width: 601px) {
    .wechat {
      display: none !important;
    }
    .mobiledevice {
      display: inline-block !important;
    }
  }
</style>

<a href="javascript:;" class="wechat" data-position="top" data-delay="50">
  <i class="fab fa-weixin"></i>
  <img class="wechat_qrcode" />
</a>

<a
  href="javascript:;"
  class="tooltipped mobiledevice"
  data-tooltip="微信: 464817304"
  data-position="top"
  data-delay="50"
>
  <i class="fab fa-weixin"></i>
</a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/blog/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>

    <div class="stars-con">
  <div id="stars"></div>
  <div id="stars2"></div>
  <div id="stars3"></div>  
</div>

<!-- 白天和黑夜主题 -->



<script>
  function switchNightMode() {
    
      setTimeout(function () {
        $('body').hasClass('DarkMode') 
        ? ($('body').removeClass('DarkMode'), localStorage.setItem('isDark', '0'), $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')) 
        : ($('body').addClass('DarkMode'), localStorage.setItem('isDark', '1'), $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')),
          
        setTimeout(function () {
          $('.Cuteen_DarkSky').fadeOut(1e3, function () {
            $(this).remove()
          })
        }, 2e3)
      })
  }
</script>
    
    
    <script>
        /* 模式判断 */
        if (localStorage.getItem('isDark') === '1') {
            document.body.classList.add('DarkMode');
            $('#sum-moon-icon').addClass("fa-sun").removeClass('fa-moon')
        } else {
            document.body.classList.remove('DarkMode');
            $('#sum-moon-icon').removeClass("fa-sun").addClass('fa-moon')
        }
    </script>

    <script src="/blog/libs/materialize/materialize.min.js"></script>
    <script src="/blog/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/blog/libs/aos/aos.js"></script>
    <script src="/blog/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/blog/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/blog/js/matery.js"></script>

    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    

    

    

	
    

    

    
    <script type="text/javascript" src="/blog/libs/background/ribbon-dynamic.js" async="async"></script>
    

    

    <!-- 冒泡 -->
    
    <script type="text/javascript">
        // 只在桌面版网页启用特效
        // var windowWidth = $(window).width();
        
            document.write('<script type="text/javascript" src="/blog/libs/others/bubleAll.js"><\/script>');
        
        
    </script>
    

    <!-- 弹出文字 -->
    

</body>

</html>
